﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Topology;

namespace HAGISV2
{
    /// <summary>
    /// 框框点信息
    /// </summary>
    [Serializable]
    public class RectangleFrame
    {
        public RectangleFrame(Coordinate leftbot,double width,double height,double rotation)
        {
            _leftBottomCoordinate = leftbot;
            _rotation = rotation;
            _width = width;
            _height = height;

            ReGenerate();
        }

        #region 方法

        public bool ReGenerate()
        {
            try
            {
                if (_fourBounderCoordinates == null) _fourBounderCoordinates = new List<Coordinate>();
                else _fourBounderCoordinates.Clear();
                if (_fourMidCoordinates == null) _fourMidCoordinates = new List<Coordinate>();
                else _fourMidCoordinates.Clear();

                Vector widthVect = new Vector(_width * Math.Cos(_rotation / 180 * Math.PI), _width * Math.Sin(_rotation / 180 * Math.PI), 0);
                Vector heighthVect = new Vector(_height * Math.Cos(_rotation / 180 * Math.PI + Math.PI / 2), _height * Math.Sin(_rotation / 180 * Math.PI + Math.PI / 2), 0);

                _fourBounderCoordinates.Add(_leftBottomCoordinate);
                _fourBounderCoordinates.Add(_leftBottomCoordinate + heighthVect);
                _fourBounderCoordinates.Add(_leftBottomCoordinate + widthVect + heighthVect);
                _fourBounderCoordinates.Add(_leftBottomCoordinate + widthVect);

                _fourMidCoordinates.Add((_fourBounderCoordinates[0] + _fourBounderCoordinates[1]) / 2);
                _fourMidCoordinates.Add((_fourBounderCoordinates[1] + _fourBounderCoordinates[2]) / 2);
                _fourMidCoordinates.Add((_fourBounderCoordinates[2] + _fourBounderCoordinates[3]) / 2);
                _fourMidCoordinates.Add((_fourBounderCoordinates[3] + _fourBounderCoordinates[0]) / 2);
            }
            catch
            {
                return false;
            }
            return true;
        }

        #endregion

        #region 私有




        #endregion

        #region 属性

        Coordinate _leftBottomCoordinate;

        public Coordinate LeftBottomCoordinate
        {
            get { return _leftBottomCoordinate; }
            set { _leftBottomCoordinate = value; }
        }

        double _width;

        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }
        double _height;

        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }


        double _rotation;

        public double Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }


        List<Coordinate> _fourBounderCoordinates;
        /// <summary>
        /// 左下，左上，右上，右下
        /// </summary>
        public List<Coordinate> FourBounderCoordinates
        {
            get { return _fourBounderCoordinates; }
        }

        List<Coordinate> _fourMidCoordinates;
        /// <summary>
        /// 左上右下边线中点
        /// </summary>
        public List<Coordinate> FourMidCoordinates
        {
            get { return _fourMidCoordinates; }
        }


        #endregion

        public RectangleFrame Clone()
        {
            RectangleFrame rf = (RectangleFrame)this.MemberwiseClone();
            rf._leftBottomCoordinate = (Coordinate)this._leftBottomCoordinate.Clone();
            return rf;
        }
    }
}
